<template>
    <div class="category-container">
        <h2 class="category-title">同志，选择你的道路吧！</h2>
        <div class="oneword">
            <p>
                {{ randomWord }}

            </p>
            <div class="like-container">
                <button 
                    class="like-button" 
                    :class="{ 'liked': isLiked }" 
                    @click="toggleLike"
                >
                    <span class="heart-icon">❤</span>
                </button>
                <div v-if="showFloatingHeart" class="floating-heart">❤</div>
            </div>
        </div>
        <div class="category-grid">
            <CategoryCard
                v-for="(category, index) in categoryList"
                :key="index"
                :category="category"
                :index="index"
                @click="handleCategoryClick"
                @hover-change="(index) => hoverIndex = index"
            />
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
import CategoryCard from '@/components/CategoryCard.vue';
import { getApiUrl } from '@/config/urlconfig';

const router = useRouter();
// const categoryList = ['reverse', 'pwn', 'web','misc'];
const categoryList = ref([]);
// const oneword = ['只有强者，才配藐视众人'];

const hoverIndex = ref(-1);
const randomWord = ref('加载中...');
const isLiked = ref(false);
const showFloatingHeart = ref(false);

const toggleLike = () => {
    isLiked.value = !isLiked.value;
    if (isLiked.value) {
        showFloatingHeart.value = true;
        setTimeout(() => {
            showFloatingHeart.value = false;
        }, 1000);
    }
};

const handleCategoryClick = (data) => {
  const encodedCategory = encodeURIComponent(data.category);
  router.push({
    name: 'folders-videos',
    query: { category: encodedCategory }
  });
};

const fetchOneWord = async () => {
  try {
    const response = await axios.get(getApiUrl('api/oneword'));
    randomWord.value = response.data.oneword;
  } catch (error) {
    console.log('获取语录失败:', response.data);
    console.error('获取语录失败:', error);
    randomWord.value = '每日一句加载失败，糟糕，服务寄了，快去找Cyber_Kaiyo';
  }
};

const fetchCategories = async () => {
  try {
    const response = await axios.get(getApiUrl('api/category'));
    if (response.data.status === 'success') {
      categoryList.value = response.data.category;
    }
  } catch (error) {
    console.error('获取分类失败:', error);
    categoryList.value = ['获取分类失败，糟糕，服务寄了，快去找Cyber_Kaiyo'];
  }
};

onMounted(async () => {
  await Promise.all([fetchOneWord(), fetchCategories()]);
});
</script>

<style scoped>
.category-container {
    max-width: 1200px;
    margin: 2rem auto;
    padding: 2rem;
}

.category-title {
    text-align: center;
    font-size: 2.5rem;
    color: var(--text-color);
    margin-bottom: 2.5rem;
    position: relative;
    display: inline-block;
    width: 100%;
}

.category-title::after {
    content: '';
    display: block;
    width: 80px;
    height: 3px;
    /* background: linear-gradient(90deg, #42b883, var(--text-color)); */
    margin: 0.5rem auto;
}

.category-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    padding: 1rem;
}

@media (max-width: 768px) {
    .category-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}
.oneword {
  max-width: 90%;
  margin: 2rem auto;
  padding: 1.5rem;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
  border-radius: 12px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.oneword p {
  font-size: 1.3rem;
  font-weight: 500;
  line-height: 1.8;
  text-align: center;
  background: linear-gradient(45deg, var(--text-color) 30%, #42b883);
  -webkit-background-clip: text;
  background-clip: text;
  color: var(--text-color);
  display: block;
  max-width: 100%;
  margin: 0;
  animation: textFloat 3s ease-in-out infinite;
  padding: 0.5rem;
  letter-spacing: 0.5px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  line-height: 1.6;
}

@keyframes textFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

.like-container {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 1rem;
    position: relative;
}

.like-button {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    transition: transform 0.2s ease;
    position: relative;
}

.like-button:hover {
    transform: scale(1.1);
}

.heart-icon {
    font-size: 1.5rem;
    color: #ff4b4b;
    opacity: 0.5;
    transition: all 0.3s ease;
}

.liked .heart-icon {
    opacity: 1;
    animation: heartBeat 0.3s ease-in-out;
}

.floating-heart {
    position: absolute;
    color: #ff4b4b;
    font-size: 1.5rem;
    pointer-events: none;
    animation: floatUp 1s ease-out forwards;
}

@keyframes heartBeat {
    0% { transform: scale(1); }
    50% { transform: scale(1.3); }
    100% { transform: scale(1); }
}

@keyframes floatUp {
    0% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
    100% {
        opacity: 0;
        transform: translateY(-50px) scale(1.5);
    }
}
</style>

